﻿function CategoryViewModel(parent) {
    var self = this;

    // knockout variables
    self.categories = ko.observableArray([]);
    self.workingCategory = ko.observable(null);
    
    //PAGING + SEARCH
    self.search = ko.observable("");
    self.currentPage = ko.observable(0);
    self.pageSize = ko.observable(10);
    self.pagesCount = ko.observable(1);
    self.count = ko.observable(0);
    self.lastPage = function () {
        self.currentPage(self.pagesCount() - 1);
    };
    self.firstPage = function () {
        self.currentPage(0);
    };
    self.nextPage = function () {
        if (self.currentPage() < self.pagesCount() - 1) {
            self.currentPage(self.currentPage() + 1);
        };
    };
    self.previousPage = function () {
        if (self.currentPage() > 0) {
            self.currentPage(self.currentPage() - 1);
        }
    };
    
    //SELECT CATEGORY
    self.selectCategory = function (category) {
        self.workingCategory(category);
    };
    self.deselectCategory = function () {
        self.workingCategory(null);
    };
    self.select = function (category) {
        self.selectCategory(category);
    };
    self.selectNew = function () {
        var newCategory = new Category({});
        self.selectCategory(newCategory);
        self.categories.push(newCategory);
    };

    //CRUD OPERATIONS
    self.saveNewCategory = function () {
        if (self.workingCategory().errors().length == 0) {
            $.ajax({
                    url: "/Category/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingCategory()),
                    contentType: 'application/json',
                    success: function(id) {
                        self.workingCategory().Id(id);
                        self.deselectCategory();
                        $("#CreateCategory").modal("hide");
                    }
                });
        }
    };
    self.saveEditCategory = function () {
        if (self.workingCategory().errors().length == 0) {
            $.ajax({
                    url: "/Category/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingCategory()),
                    contentType: 'application/json',
                    success: function() {
                        self.deselectCategory();
                        $("#EditCategory").modal("hide");
                    }
                });
        }
    };
    self.deleteCategory = function () {
        $.post("/Category/DeleteCategory", { id: self.workingCategory().Id() }, function () {
            self.categories.pop(self.workingCategory());
            self.deselectCategory();
        });
        $('#DeleteCategory').modal('hide');
    };
    self.cancelNewCategory = function () {
        self.deselectCategory();
        self.categories.pop(self.workingCategory);
        $('#CreateCategory').modal('hide');
    };
    self.cancelEditCategory = function () {
        self.deselectCategory();
        self.loadCategories();
        $('#EditCategory').modal('hide');
    };
    self.cancelDetailsCategory = function () {
        self.deselectCategory();
        $('#DetailsCategory').modal('hide');
    };
    self.cancelDeleteCategory = function () {
        self.deselectCategory();
        $('#DeleteCategory').modal('hide');
    };
    self.loadCategories = function (pageIndex, pageSize, search) {
        $.getJSON("/Category/List", { search: search, pageIndex: pageIndex, pageSize: pageSize }, function (categories) {
            self.pagesCount(categories.PagesCount);
            self.count(categories.Count);
            var mappedCategories = $.map(categories.Entities, function (item) { return new Category(item); });
            self.categories(mappedCategories);
        });
    };
    self.triggerLoadCategories = ko.computed(function () {
        self.currentPage();
        self.pageSize();
        var search = self.search();
        if (parent.route() == "categories") {
            var route = '#/Categories/List/?pageIndex=' + self.currentPage() + '&pageSize=' + self.pageSize();
            if (search != null && search != "") {
                route = route + '&search=' + self.search();
            }
            location.hash = route;
        }
    }, this);
}